package com.bascker.algorithm.practice.str;

import java.util.Objects;

/**
 * 字符串中的单词数: 统计字符串中的单词个数，这里的单词指的是连续的不是空格的字符。
 *
 * in: "Hello, my name is John"    out: 5
 * 说明："Hello," 算作 1 个单词。
 *
 * https://gitee.com/bascker/algorithm/wikis/2%E3%80%81%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E5%8D%95%E8%AF%8D%E6%95%B0?sort_id=2972454
 *
 * @author bascker
 * @apiNote from leetcode T434
 */
public class CountSegments {

    private static final char BLANK = ' ';

    public int countSegments(String s) {
        if (Objects.isNull(s) || s.trim().length() == 0) {
            return 0;
        }

        int count = 0;
        for (int i = 0; i < s.length(); i ++) {
            if ((i == 0 && s.charAt(i) != BLANK)
                    || (s.charAt(i) != BLANK && s.charAt(i - 1) == BLANK))  {
                count ++;
            }

        }
        return count;
    }

}
